package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.BoardMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        //非空检验
        if(num<=0){
            log.warn("num不能小于0!");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //查询数据库
        List<Board> result =boardMapper.selectByNum(num);
        return result;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(null==id || id<=0){
            log.warn("传入的要修改的板块id有误! id = {}",id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应的版块
        Board board = boardMapper.selectByPrimaryKey(id);
        if (null==board){
            log.error("当前板块不存在! id={}",id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXIST));
        }
        Board updateBoard = new Board();
        //因为是动态SQL,所以只需要有主键和要修改的值就行了
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row!=1){
            log.error("添加帖子失败! 受影响的行数不为1!");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        if(null == id || id<=0){
            log.warn("当前要查询的板块不存在!");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXIST));
        }
        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if(null == id){
            log.warn("被删除帖子的板块id传入为空!, id:{}",id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(null==board){
            log.warn("当前板块不存在! id:{}",id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXIST));
        }
        Board updateboard = new Board();
        updateboard.setId(id);
        updateboard.setArticleCount(board.getArticleCount()-1);
        if(board.getArticleCount()-1<0){
            updateboard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateboard);
        if(row!=1){
            log.error("删除板块中的帖子失败!,boardId:{}",id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_DELETE));
        }
    }
}
